package com.biostime.dingdingbox.modules.db.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.biostime.dingdingbox.common.api.FastResponse;
import com.biostime.dingdingbox.common.api.ObjResponse;
import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.dto.MemberDto;
import com.biostime.dingdingbox.modules.db.entity.BoxCode;
import com.biostime.dingdingbox.modules.db.entity.MemberBox;
import com.biostime.dingdingbox.modules.db.service.BoxCodeService;
import com.biostime.dingdingbox.modules.db.service.HttpRequestService;
import com.biostime.dingdingbox.modules.db.service.MemberBoxService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * 激活码API
 * @author chenweicheng
 * @version 2016-07-01
 */
@RestController
@RequestMapping(value = "${adminPath}/db/api/code")
public class CodeApi extends BaseController {
		
	@Autowired
	private MemberBoxService memberBoxService;
	
	@Autowired
	private BoxCodeService boxCodeService;
	
	
	/**
	 * 激活
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	@RequestMapping(value = "activate")
	public ObjResponse<Map<String,Object>> activate(String code,String userId , HttpServletRequest request , HttpServletResponse response) throws 
			JsonParseException, JsonMappingException, IOException {	
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<String,Object>();
		String message = "验证成功";				
		int errCode = 0;
		if(userId != null && !userId.equals("") && code != null){
					
			//父母能量会员编号
			String memberId = userId;
						
			//通过激活码查找魔法盒
			BoxCode boxCode = boxCodeService.apiGetByCode(code);
			if(boxCode != null){
								
				//通过激活码和会员编号查找激活记录
				MemberBox box = memberBoxService.apiGetByForeign(memberId, code);
				if(box == null){			
					 errCode = memberBoxService.apiInsert(userId, boxCode); 					
				}
				if(errCode == 0){
					map.put("boxId", boxCode.getBoxId());		
				}else{
					message = "验证失败,不存在该用户";
				}
				
			}else{
				message = "验证失败,激活码无效";
				errCode = 1;
			}
			
		}else{
			message = "验证失败,参数不全";
			errCode = 1;
		}
				
		return  FastResponse.create().errMsg(message).errCode(errCode).obj(map);		
	}
	
	@RequestMapping(value = "test")
	public Object test() throws JsonParseException, JsonMappingException, IOException{
		String str = new String(HttpRequestService.sendGet(Global.getConfig("ParentPowerServer")+
				"/qdingding-webapp/a/qd/api/out/getMemberInfo", "userId=638"));		
		ObjectMapper mapper = new ObjectMapper();
		ObjResponse<MemberDto> entity = mapper.readValue(str,new TypeReference<ObjResponse<MemberDto>>(){});		
		MemberDto m = entity.getObj();
		System.out.println(m);
		return entity;
	}
		
}
